
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>concurrent.futures --- 启动并行任务 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="subprocess --- 子进程管理" href="subprocess.html" />
    <link rel="prev" title="concurrent 包" href="concurrent.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/concurrent.futures.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="subprocess.html" title="subprocess --- 子进程管理"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="concurrent.html" title="concurrent 包"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="concurrency.html" accesskey="U">并发执行</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-concurrent.futures">
<span id="concurrent-futures-launching-parallel-tasks"></span><h1><a class="reference internal" href="#module-concurrent.futures" title="concurrent.futures: Execute computations concurrently using threads or processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> --- 启动并行任务<a class="headerlink" href="#module-concurrent.futures" title="永久链接至标题">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
<p><strong>源码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/concurrent/futures/thread.py">Lib/concurrent/futures/thread.py</a> 和 <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/concurrent/futures/process.py">Lib/concurrent/futures/process.py</a></p>
<hr class="docutils" />
<p><a class="reference internal" href="#module-concurrent.futures" title="concurrent.futures: Execute computations concurrently using threads or processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> 模块提供异步执行可调用对象高层接口。</p>
<p>异步执行可以由 <a class="reference internal" href="#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a> 使用线程或由 <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> 使用单独的进程来实现。 两者都是实现抽像类 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 定义的接口。</p>
<div class="section" id="executor-objects">
<h2>执行器对象<a class="headerlink" href="#executor-objects" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="concurrent.futures.Executor">
<em class="property">class </em><code class="sig-prename descclassname">concurrent.futures.</code><code class="sig-name descname">Executor</code><a class="headerlink" href="#concurrent.futures.Executor" title="永久链接至目标">¶</a></dt>
<dd><p>抽象类提供异步执行调用方法。要通过它的子类调用，而不是直接调用。</p>
<blockquote>
<div><dl class="method">
<dt id="concurrent.futures.Executor.submit">
<code class="sig-name descname">submit</code><span class="sig-paren">(</span><em class="sig-param">fn</em>, <em class="sig-param">*args</em>, <em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Executor.submit" title="永久链接至目标">¶</a></dt>
<dd><p>调度可调用对象 <em>fn</em>，以 <code class="docutils literal notranslate"><span class="pre">fn(*args</span> <span class="pre">**kwargs)</span></code> 方式执行并返回 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 对像代表可调用对象的执行。:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="k">as</span> <span class="n">executor</span><span class="p">:</span>
    <span class="n">future</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="nb">pow</span><span class="p">,</span> <span class="mi">323</span><span class="p">,</span> <span class="mi">1235</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">())</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="concurrent.futures.Executor.map">
<code class="sig-name descname">map</code><span class="sig-paren">(</span><em class="sig-param">func</em>, <em class="sig-param">*iterables</em>, <em class="sig-param">timeout=None</em>, <em class="sig-param">chunksize=1</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Executor.map" title="永久链接至目标">¶</a></dt>
<dd><p>类似于 <a class="reference internal" href="functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map(func,</span> <span class="pre">*iterables)</span></code></a> 除去：</p>
<ul class="simple">
<li><p>应立即收集 <em>iterables</em> 不要延迟再收集;</p></li>
<li><p><em>func</em> 是异步执行的以及对 <em>func</em> 的调用可以同时执行。</p></li>
</ul>
<p>如果从原始调用到 <a class="reference internal" href="#concurrent.futures.Executor.map" title="concurrent.futures.Executor.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.map()</span></code></a> 经过 <em>timeout</em> 秒后， <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> 已被调用且返回的结果还不可用，那么已返回的迭代器将触发 <a class="reference internal" href="#concurrent.futures.TimeoutError" title="concurrent.futures.TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">concurrent.futures.TimeoutError</span></code></a>  。 <em>timeout</em> 可以是整数或浮点数。如果 <em>timeout</em> 没有指定或为 <code class="docutils literal notranslate"><span class="pre">None</span></code> ，则没有超时限制。</p>
<p>如果 <em>func</em> 调用引发一个异常，当从迭代器中取回它的值时这个异常将被引发。</p>
<p>使用 <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> 时，这个方法会将 <em>iterables</em> 分割任务块并作为独立的任务并提交到执行池中。这些块的大概数量可以由 <em>chunksize</em> 指定正整数设置。 对很长的迭代器来说，使用大的 <em>chunksize</em> 值比默认值 1 能显著地提高性能。 <em>chunksize</em> 对 <a class="reference internal" href="#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a> 没有效果。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>加入 <em>chunksize</em> 参数。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="concurrent.futures.Executor.shutdown">
<code class="sig-name descname">shutdown</code><span class="sig-paren">(</span><em class="sig-param">wait=True</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Executor.shutdown" title="永久链接至目标">¶</a></dt>
<dd><p>当待执行的期程完成执行后向执行者发送信号，它就会释放正在使用的任何资源。调用 <a class="reference internal" href="#concurrent.futures.Executor.submit" title="concurrent.futures.Executor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.submit()</span></code></a> 和 <a class="reference internal" href="#concurrent.futures.Executor.submit" title="concurrent.futures.Executor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.submit()</span></code></a> 会在关闭后触发 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>。</p>
<p>如果 <em>wait</em> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 则此方法只有在所有待执行的期程完成执行且释放已分配的资源后才会返回。 如果 <em>wait</em> 为 <code class="docutils literal notranslate"><span class="pre">False</span></code>，方法立即返回，所有待执行的期程完成执行后会释放已分配的资源。 不管 <em>wait</em> 的值是什么，整个 Python 程序将等到所有待执行的期程完成执行后才退出。</p>
<p>如果使用 <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 语句，你就可以避免显式调用这个方法，它将会停止 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> (就好像 <a class="reference internal" href="#concurrent.futures.Executor.shutdown" title="concurrent.futures.Executor.shutdown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.shutdown()</span></code></a> 调用时 <em>wait</em> 设为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 一样等待):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">shutil</span>
<span class="k">with</span> <span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
    <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src1.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest1.txt&#39;</span><span class="p">)</span>
    <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src2.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest2.txt&#39;</span><span class="p">)</span>
    <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src3.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest3.txt&#39;</span><span class="p">)</span>
    <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src4.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest4.txt&#39;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

</div></blockquote>
</dd></dl>

</div>
<div class="section" id="threadpoolexecutor">
<h2>线程池执行器<a class="headerlink" href="#threadpoolexecutor" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a> 是 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 的子类，它使用线程池来异步执行调用。</p>
<p>当回调已关联了一个 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 然后再等待另一个 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 的结果时就会发产死锁情况。例如:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
<span class="k">def</span> <span class="nf">wait_on_b</span><span class="p">():</span>
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">result</span><span class="p">())</span>  <span class="c1"># b will never complete because it is waiting on a.</span>
    <span class="k">return</span> <span class="mi">5</span>

<span class="k">def</span> <span class="nf">wait_on_a</span><span class="p">():</span>
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">result</span><span class="p">())</span>  <span class="c1"># a will never complete because it is waiting on b.</span>
    <span class="k">return</span> <span class="mi">6</span>


<span class="n">executor</span> <span class="o">=</span> <span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">wait_on_b</span><span class="p">)</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">wait_on_a</span><span class="p">)</span>
</pre></div>
</div>
<p>与:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">wait_on_future</span><span class="p">():</span>
    <span class="n">f</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="nb">pow</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
    <span class="c1"># This will never complete because there is only one worker thread and</span>
    <span class="c1"># it is executing this function.</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">result</span><span class="p">())</span>

<span class="n">executor</span> <span class="o">=</span> <span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">wait_on_future</span><span class="p">)</span>
</pre></div>
</div>
<dl class="class">
<dt id="concurrent.futures.ThreadPoolExecutor">
<em class="property">class </em><code class="sig-prename descclassname">concurrent.futures.</code><code class="sig-name descname">ThreadPoolExecutor</code><span class="sig-paren">(</span><em class="sig-param">max_workers=None</em>, <em class="sig-param">thread_name_prefix=''</em>, <em class="sig-param">initializer=None</em>, <em class="sig-param">initargs=()</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.ThreadPoolExecutor" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 子类使用最多 <em>max_workers</em> 个线程的线程池来异步执行调用。</p>
<p><em>initializer</em> 是在每个工作者线程开始处调用的一个可选可调用对象。 <em>initargs</em> 是传递给初始化器的元组参数。任何向池提交更多工作的尝试， <em>initializer</em> 都将引发一个异常，当前所有等待的工作都会引发一个 <a class="reference internal" href="#concurrent.futures.thread.BrokenThreadPool" title="concurrent.futures.thread.BrokenThreadPool"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenThreadPool</span></code></a>。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>如果 <em>max_workers</em> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或没有指定，将默认为机器处理器的个数，假如 <a class="reference internal" href="#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a> 则重于I/O操作而不是CPU运算，那么可以乘以 <code class="docutils literal notranslate"><span class="pre">5</span></code> ，同时工作线程的数量可以比 <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> 的数量高。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能: </span>添加 <em>thread_name_prefix</em> 参数允许用户控制由线程池创建的 <a class="reference internal" href="threading.html#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Thread</span></code></a> 工作线程名称以方便调试。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>加入 <em>initializer</em> 和*initargs* 参数。</p>
</div>
</dd></dl>

<div class="section" id="threadpoolexecutor-example">
<span id="id1"></span><h3>ThreadPoolExecutor 例子<a class="headerlink" href="#threadpoolexecutor-example" title="永久链接至标题">¶</a></h3>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">concurrent.futures</span>
<span class="kn">import</span> <span class="nn">urllib.request</span>

<span class="n">URLS</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;http://www.foxnews.com/&#39;</span><span class="p">,</span>
        <span class="s1">&#39;http://www.cnn.com/&#39;</span><span class="p">,</span>
        <span class="s1">&#39;http://europe.wsj.com/&#39;</span><span class="p">,</span>
        <span class="s1">&#39;http://www.bbc.co.uk/&#39;</span><span class="p">,</span>
        <span class="s1">&#39;http://some-made-up-domain.com/&#39;</span><span class="p">]</span>

<span class="c1"># Retrieve a single page and report the URL and contents</span>
<span class="k">def</span> <span class="nf">load_url</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">timeout</span><span class="p">):</span>
    <span class="k">with</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">)</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">conn</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>

<span class="c1"># We can use a with statement to ensure threads are cleaned up promptly</span>
<span class="k">with</span> <span class="n">concurrent</span><span class="o">.</span><span class="n">futures</span><span class="o">.</span><span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span> <span class="k">as</span> <span class="n">executor</span><span class="p">:</span>
    <span class="c1"># Start the load operations and mark each future with its URL</span>
    <span class="n">future_to_url</span> <span class="o">=</span> <span class="p">{</span><span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">load_url</span><span class="p">,</span> <span class="n">url</span><span class="p">,</span> <span class="mi">60</span><span class="p">):</span> <span class="n">url</span> <span class="k">for</span> <span class="n">url</span> <span class="ow">in</span> <span class="n">URLS</span><span class="p">}</span>
    <span class="k">for</span> <span class="n">future</span> <span class="ow">in</span> <span class="n">concurrent</span><span class="o">.</span><span class="n">futures</span><span class="o">.</span><span class="n">as_completed</span><span class="p">(</span><span class="n">future_to_url</span><span class="p">):</span>
        <span class="n">url</span> <span class="o">=</span> <span class="n">future_to_url</span><span class="p">[</span><span class="n">future</span><span class="p">]</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%r</span><span class="s1"> generated an exception: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">exc</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%r</span><span class="s1"> page is </span><span class="si">%d</span><span class="s1"> bytes&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)))</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="processpoolexecutor">
<h2>进程池执行器<a class="headerlink" href="#processpoolexecutor" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> 是 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 的子类，它使用进程池来实现异步执行调用。 <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> 使用 <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> 回避 <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">Global Interpreter Lock</span></a> 但也意味着只可以处理和返回可序列化的对象。</p>
<p><code class="docutils literal notranslate"><span class="pre">__main__</span></code> 模块必须可以被工作者子进程导入。这意味着 <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> 不可以工作在交互式解释器中。</p>
<p>从可调用对象中调用 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 或 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 的方法提交给 <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> 会导致死锁。</p>
<dl class="class">
<dt id="concurrent.futures.ProcessPoolExecutor">
<em class="property">class </em><code class="sig-prename descclassname">concurrent.futures.</code><code class="sig-name descname">ProcessPoolExecutor</code><span class="sig-paren">(</span><em class="sig-param">max_workers=None</em>, <em class="sig-param">mp_context=None</em>, <em class="sig-param">initializer=None</em>, <em class="sig-param">initargs=()</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.ProcessPoolExecutor" title="永久链接至目标">¶</a></dt>
<dd><p>异步执行调用的 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 子类使用一个最多有 <em>max_workers</em> 个进程的进程池。 如果 <em>max_workers</em> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或未给出，它将默认为机器的处理器个数。 如果 <em>max_workers</em> 小于等于 <code class="docutils literal notranslate"><span class="pre">0</span></code>，则将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。 在 Windows 上，<em>max_workers</em> 必须小于等于 <code class="docutils literal notranslate"><span class="pre">61</span></code>，否则将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。 如果 <em>max_workers</em> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则所选择的默认最多为 <code class="docutils literal notranslate"><span class="pre">61</span></code>，即使存在更多处理器。 <em>mp_context</em> 可以是一个多进程上下文或是 None。 它将被用来启动工作者。 如果 <em>mp_context</em> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或未给出，将使用默认的多进程上下文。</p>
<p><em>initializer</em> 是在每个工作者进程开始处调用的一个可选可调用对象。 <em>initargs</em> 是传递给初始化器的元组参数。任何向池提交更多工作的尝试， <em>initializer</em> 都将引发一个异常，当前所有等待的工作都会引发一个 <code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenProcessPool</span></code>。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>如果其中一个工作进程被突然终止，<code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenProcessPool</span></code> 就会马上触发。可预计的行为没有定义，但执行器上的操作或它的期程会被冻结或死锁。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>添加 <em>mp_context</em> 参数允许用户控制由进程池创建给工作者进程的开始方法 。</p>
<p>加入 <em>initializer</em> 和*initargs* 参数。</p>
</div>
</dd></dl>

<div class="section" id="processpoolexecutor-example">
<span id="id2"></span><h3>ProcessPoolExecutor 例子<a class="headerlink" href="#processpoolexecutor-example" title="永久链接至标题">¶</a></h3>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">concurrent.futures</span>
<span class="kn">import</span> <span class="nn">math</span>

<span class="n">PRIMES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="mi">112272535095293</span><span class="p">,</span>
    <span class="mi">112582705942171</span><span class="p">,</span>
    <span class="mi">112272535095293</span><span class="p">,</span>
    <span class="mi">115280095190773</span><span class="p">,</span>
    <span class="mi">115797848077099</span><span class="p">,</span>
    <span class="mi">1099726899285419</span><span class="p">]</span>

<span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="n">sqrt_n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">n</span><span class="p">)))</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">sqrt_n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
    <span class="k">return</span> <span class="kc">True</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="k">with</span> <span class="n">concurrent</span><span class="o">.</span><span class="n">futures</span><span class="o">.</span><span class="n">ProcessPoolExecutor</span><span class="p">()</span> <span class="k">as</span> <span class="n">executor</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">number</span><span class="p">,</span> <span class="n">prime</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">PRIMES</span><span class="p">,</span> <span class="n">executor</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">is_prime</span><span class="p">,</span> <span class="n">PRIMES</span><span class="p">)):</span>
            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%d</span><span class="s1"> is prime: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">number</span><span class="p">,</span> <span class="n">prime</span><span class="p">))</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="future-objects">
<h2>期程对象<a class="headerlink" href="#future-objects" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 类将可调用对象封装为异步执行。<a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 实例由 <a class="reference internal" href="#concurrent.futures.Executor.submit" title="concurrent.futures.Executor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.submit()</span></code></a> 创建。</p>
<dl class="class">
<dt id="concurrent.futures.Future">
<em class="property">class </em><code class="sig-prename descclassname">concurrent.futures.</code><code class="sig-name descname">Future</code><a class="headerlink" href="#concurrent.futures.Future" title="永久链接至目标">¶</a></dt>
<dd><p>将可调用对象封装为异步执行。<a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 实例由 <a class="reference internal" href="#concurrent.futures.Executor.submit" title="concurrent.futures.Executor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.submit()</span></code></a> 创建，除非测试，不应直接创建。</p>
<blockquote>
<div><dl class="method">
<dt id="concurrent.futures.Future.cancel">
<code class="sig-name descname">cancel</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.cancel" title="永久链接至目标">¶</a></dt>
<dd><p>尝试取消调用。 如果调用正在执行或已结束运行不能被取消则该方法将返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>，否则调用会被取消并且该方法将返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="concurrent.futures.Future.cancelled">
<code class="sig-name descname">cancelled</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.cancelled" title="永久链接至目标">¶</a></dt>
<dd><p>如果调用成功取消返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="concurrent.futures.Future.running">
<code class="sig-name descname">running</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.running" title="永久链接至目标">¶</a></dt>
<dd><p>如果调用正在执行而且不能被取消那么返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> 。</p>
</dd></dl>

<dl class="method">
<dt id="concurrent.futures.Future.done">
<code class="sig-name descname">done</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.done" title="永久链接至目标">¶</a></dt>
<dd><p>如果调用已被取消或正常结束那么返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="concurrent.futures.Future.result">
<code class="sig-name descname">result</code><span class="sig-paren">(</span><em class="sig-param">timeout=None</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.result" title="永久链接至目标">¶</a></dt>
<dd><p>返回调用返回的值。如果调用还没完成那么这个方法将等待 <em>timeout</em> 秒。如果在 <em>timeout</em> 秒内没有执行完成，<a class="reference internal" href="#concurrent.futures.TimeoutError" title="concurrent.futures.TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">concurrent.futures.TimeoutError</span></code></a> 将会被触发。<em>timeout</em> 可以是整数或浮点数。如果 <em>timeout</em> 没有指定或为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，那么等待时间就没有限制。</p>
<p>如果期程在完成前被取消， <a class="reference internal" href="#concurrent.futures.CancelledError" title="concurrent.futures.CancelledError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CancelledError</span></code></a> 将被触发。</p>
<p>如果调用引发了一个异常，这个方法也会引发同样的异常。</p>
</dd></dl>

<dl class="method">
<dt id="concurrent.futures.Future.exception">
<code class="sig-name descname">exception</code><span class="sig-paren">(</span><em class="sig-param">timeout=None</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.exception" title="永久链接至目标">¶</a></dt>
<dd><p>返回由调用引发的异常。如果调用还没完成那么这个方法将等待 <em>timeout</em> 秒。如果在 <em>timeout</em> 秒内没有执行完成，<a class="reference internal" href="#concurrent.futures.TimeoutError" title="concurrent.futures.TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">concurrent.futures.TimeoutError</span></code></a> 将会被触发。<em>timeout</em> 可以是整数或浮点数。如果 <em>timeout</em> 没有指定或为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，那么等待时间就没有限制。</p>
<p>如果期程在完成前被取消， <a class="reference internal" href="#concurrent.futures.CancelledError" title="concurrent.futures.CancelledError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CancelledError</span></code></a> 将被触发。</p>
<p>如果调用正常完成那么返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="concurrent.futures.Future.add_done_callback">
<code class="sig-name descname">add_done_callback</code><span class="sig-paren">(</span><em class="sig-param">fn</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.add_done_callback" title="永久链接至目标">¶</a></dt>
<dd><p>附加可调用 <em>fn</em> 到期程。当期程被取消或完成运行时，将会调用 <em>fn</em>，而这个期程将作为它唯一的参数。</p>
<p>加入的可调用对象总被属于添加它们的进程中的线程按加入的顺序调用。如果可调用对象引发一个 <a class="reference internal" href="exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> 子类，它会被记录下来并被忽略掉。如果可调用对象引发一个 <a class="reference internal" href="exceptions.html#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> 子类，这个行为没有定义。</p>
<p>如果期程已经完成或已取消，<em>fn</em> 会被立即调用。</p>
</dd></dl>

</div></blockquote>
<p>下面这些 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 方法用于单元测试和 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 实现。</p>
<blockquote>
<div><dl class="method">
<dt id="concurrent.futures.Future.set_running_or_notify_cancel">
<code class="sig-name descname">set_running_or_notify_cancel</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.set_running_or_notify_cancel" title="永久链接至目标">¶</a></dt>
<dd><p>这个方法只可以在执行关联 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a>  工作之前由 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 实现调用或由单测试调用。</p>
<p>如果这个方法返回  <code class="docutils literal notranslate"><span class="pre">False</span></code> 那么 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 已被取消，即 <a class="reference internal" href="#concurrent.futures.Future.cancel" title="concurrent.futures.Future.cancel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Future.cancel()</span></code></a> 已被调用并返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> 。等待 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 完成 (即通过 <a class="reference internal" href="#concurrent.futures.as_completed" title="concurrent.futures.as_completed"><code class="xref py py-func docutils literal notranslate"><span class="pre">as_completed()</span></code></a> 或 <a class="reference internal" href="#concurrent.futures.wait" title="concurrent.futures.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait()</span></code></a>) 的线程将被唤醒。</p>
<p>如果这个方法返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> 那么 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 不会被取消并已将它变为正在运行状态，也就是说调用 <a class="reference internal" href="#concurrent.futures.Future.running" title="concurrent.futures.Future.running"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Future.running()</span></code></a> 时将返回 <cite>True</cite>。</p>
<p>这个方法只可以被调用一次并且不能在调用 <a class="reference internal" href="#concurrent.futures.Future.set_result" title="concurrent.futures.Future.set_result"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Future.set_result()</span></code></a> 或 <a class="reference internal" href="#concurrent.futures.Future.set_exception" title="concurrent.futures.Future.set_exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Future.set_exception()</span></code></a> 之后再调用。</p>
</dd></dl>

<dl class="method">
<dt id="concurrent.futures.Future.set_result">
<code class="sig-name descname">set_result</code><span class="sig-paren">(</span><em class="sig-param">result</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.set_result" title="永久链接至目标">¶</a></dt>
<dd><p>设置将 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 关联工作的结果给 <em>result</em> 。</p>
<p>这个方法只可以由 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 实现和单元测试使用。</p>
</dd></dl>

<dl class="method">
<dt id="concurrent.futures.Future.set_exception">
<code class="sig-name descname">set_exception</code><span class="sig-paren">(</span><em class="sig-param">exception</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.set_exception" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 关联工作的结果给 <a class="reference internal" href="exceptions.html#Exception" title="Exception"><code class="xref py py-class docutils literal notranslate"><span class="pre">Exception</span></code></a> <em>exception</em> 。</p>
<p>这个方法只可以由 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 实现和单元测试使用。</p>
</dd></dl>

</div></blockquote>
</dd></dl>

</div>
<div class="section" id="module-functions">
<h2>模块函数<a class="headerlink" href="#module-functions" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="concurrent.futures.wait">
<code class="sig-prename descclassname">concurrent.futures.</code><code class="sig-name descname">wait</code><span class="sig-paren">(</span><em class="sig-param">fs</em>, <em class="sig-param">timeout=None</em>, <em class="sig-param">return_when=ALL_COMPLETED</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.wait" title="永久链接至目标">¶</a></dt>
<dd><p>等待 <em>fs</em> 指定的 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 实例（可能由不同的 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 实例创建）完成。 返回一个由集合构成的具名  2 元组。 第一个集合名称为 <code class="docutils literal notranslate"><span class="pre">done</span></code>，包含在等待完成之前已完成的期程（包括正常结束或被取消的期程）。 第二个集合名称为 <code class="docutils literal notranslate"><span class="pre">not_done</span></code>，包含未完成的期程（包括挂起的或正在运行的期程）。</p>
<p><em>timeout</em> 可以用来控制返回前最大的等待秒数。 <em>timeout</em> 可以为 int 或 float 类型。 如果 <em>timeout</em> 未指定或为 <code class="docutils literal notranslate"><span class="pre">None</span></code> ，则不限制等待时间。</p>
<p><em>return_when</em> 指定此函数应在何时返回。它必须为以下常数之一:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 42%" />
<col style="width: 58%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>常数</p></th>
<th class="head"><p>描述</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FIRST_COMPLETED</span></code></p></td>
<td><p>函数将在任意可等待对象结束或取消时返回。</p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FIRST_EXCEPTION</span></code></p></td>
<td><p>函数将在任意可等待对象因引发异常而结束时返回。当没有引发任何异常时它就相当于 <code class="xref py py-const docutils literal notranslate"><span class="pre">ALL_COMPLETED</span></code>。</p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">ALL_COMPLETED</span></code></p></td>
<td><p>函数将在所有可等待对象结束或取消时返回。</p></td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="concurrent.futures.as_completed">
<code class="sig-prename descclassname">concurrent.futures.</code><code class="sig-name descname">as_completed</code><span class="sig-paren">(</span><em class="sig-param">fs</em>, <em class="sig-param">timeout=None</em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.as_completed" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个包含 <em>fs</em> 所指定的 <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 实例（可能由不同的 <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> 实例创建）的迭代器，这些实例会在完成时生成期程（包括正常结束或被取消的期程）。 任何由 <em>fs</em> 所指定的重复期程将只被返回一次。 任何在 <a class="reference internal" href="#concurrent.futures.as_completed" title="concurrent.futures.as_completed"><code class="xref py py-func docutils literal notranslate"><span class="pre">as_completed()</span></code></a> 被调用之前完成的期程将优先被生成。 如果 <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> 被调用并且在对 <a class="reference internal" href="#concurrent.futures.as_completed" title="concurrent.futures.as_completed"><code class="xref py py-func docutils literal notranslate"><span class="pre">as_completed()</span></code></a> 的原始调用 <em>timeout</em> 秒之后结果仍不可用，则返回的迭代器将引发 <a class="reference internal" href="#concurrent.futures.TimeoutError" title="concurrent.futures.TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">concurrent.futures.TimeoutError</span></code></a>。 <em>timeout</em> 可以为整数或浮点数。 如果 <em>timeout</em> 未指定或为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则不限制等待时间。</p>
</dd></dl>

<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3148"><strong>PEP 3148</strong></a> -- 期程 - 异步执行指令。</dt><dd><p>该提案描述了Python标准库中包含的这个特性。</p>
</dd>
</dl>
</div>
</div>
<div class="section" id="exception-classes">
<h2>Exception类<a class="headerlink" href="#exception-classes" title="永久链接至标题">¶</a></h2>
<dl class="exception">
<dt id="concurrent.futures.CancelledError">
<em class="property">exception </em><code class="sig-prename descclassname">concurrent.futures.</code><code class="sig-name descname">CancelledError</code><a class="headerlink" href="#concurrent.futures.CancelledError" title="永久链接至目标">¶</a></dt>
<dd><p>期程被取消时会触发。</p>
</dd></dl>

<dl class="exception">
<dt id="concurrent.futures.TimeoutError">
<em class="property">exception </em><code class="sig-prename descclassname">concurrent.futures.</code><code class="sig-name descname">TimeoutError</code><a class="headerlink" href="#concurrent.futures.TimeoutError" title="永久链接至目标">¶</a></dt>
<dd><p>期程运算超出给定的超时数值时引发。</p>
</dd></dl>

<dl class="exception">
<dt id="concurrent.futures.BrokenExecutor">
<em class="property">exception </em><code class="sig-prename descclassname">concurrent.futures.</code><code class="sig-name descname">BrokenExecutor</code><a class="headerlink" href="#concurrent.futures.BrokenExecutor" title="永久链接至目标">¶</a></dt>
<dd><p>当执行器被某些原因中断而且不能用来提交或执行新任务时就会被引发派生于 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> 的异常类。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="exception">
<dt id="concurrent.futures.thread.BrokenThreadPool">
<em class="property">exception </em><code class="sig-prename descclassname">concurrent.futures.thread.</code><code class="sig-name descname">BrokenThreadPool</code><a class="headerlink" href="#concurrent.futures.thread.BrokenThreadPool" title="永久链接至目标">¶</a></dt>
<dd><p>当 <code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code> 中的其中一个工作者初始化失败时会引发派生于 <a class="reference internal" href="#concurrent.futures.BrokenExecutor" title="concurrent.futures.BrokenExecutor"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenExecutor</span></code></a> 的异常类。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="exception">
<dt id="concurrent.futures.process.BrokenProcessPool">
<em class="property">exception </em><code class="sig-prename descclassname">concurrent.futures.process.</code><code class="sig-name descname">BrokenProcessPool</code><a class="headerlink" href="#concurrent.futures.process.BrokenProcessPool" title="永久链接至目标">¶</a></dt>
<dd><p>当 <code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code> 中的其中一个工作者不完整终止时(比如，被外部杀死)会引发派生于 <a class="reference internal" href="#concurrent.futures.BrokenExecutor" title="concurrent.futures.BrokenExecutor"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenExecutor</span></code></a> ( 原名 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> ) 的异常类。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code> --- 启动并行任务</a><ul>
<li><a class="reference internal" href="#executor-objects">执行器对象</a></li>
<li><a class="reference internal" href="#threadpoolexecutor">线程池执行器</a><ul>
<li><a class="reference internal" href="#threadpoolexecutor-example">ThreadPoolExecutor 例子</a></li>
</ul>
</li>
<li><a class="reference internal" href="#processpoolexecutor">进程池执行器</a><ul>
<li><a class="reference internal" href="#processpoolexecutor-example">ProcessPoolExecutor 例子</a></li>
</ul>
</li>
<li><a class="reference internal" href="#future-objects">期程对象</a></li>
<li><a class="reference internal" href="#module-functions">模块函数</a></li>
<li><a class="reference internal" href="#exception-classes">Exception类</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="concurrent.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent</span></code> 包</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="subprocess.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> --- 子进程管理</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/concurrent.futures.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="subprocess.html" title="subprocess --- 子进程管理"
             >下一页</a> |</li>
        <li class="right" >
          <a href="concurrent.html" title="concurrent 包"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="concurrency.html" >并发执行</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>